package com.xls.aclservice.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xls.aclservice.entity.SysUser;
import com.xls.base.annotation.Log;
import com.xls.commonutils.Result;
import com.xls.aclservice.service.SysUserService;
import com.xls.aclservice.vo.UserQuery;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xls
 * @since 2021-03-23
 */
@RestController
@RequestMapping("/admin/acl/user")
@Api(value="用户接口")
@Slf4j
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Log(title = "用户管理", businessType = "查询")
    @PreAuthorize("hasAnyAuthority('system:sysUser:list')")
    @GetMapping("/getUser/{id}")
    public Result<SysUser> getUser(@PathVariable Long id) {
        return Result.success(sysUserService.getUser(id));
    }

    @Log(title = "用户管理", businessType = "查询")
    @GetMapping("/getUserByName/{username}")
    public Result<SysUser> getUserByName(@PathVariable String username) {
        return Result.success(sysUserService.selectByUsername(username));
    }

    @Log(title = "用户管理", businessType = "查询")
    @PreAuthorize("hasAnyAuthority('system:sysUser:list')")
    @PostMapping("/list/{page}/{limit}")
    public Result<Page<SysUser>> list(@PathVariable Integer page, @PathVariable Integer limit,
                                      @RequestBody UserQuery userQuery) {
        return Result.success(sysUserService.getUserPage(page, limit, userQuery));
    }

    @Log(title = "用户管理", businessType = "新增")
    @PreAuthorize("hasAnyAuthority('system:sysUser:save')")
    @PostMapping("/save")
    public Result<SysUser> save(@RequestBody SysUser sysUser) {
        sysUserService.saveUser(sysUser);
        return Result.success(sysUser);
    }

    @Log(title = "用户管理", businessType = "修改")
    @PreAuthorize("hasAnyAuthority('system:sysUser:update')")
    @PostMapping("/update")
    public Result<SysUser> update(@RequestBody SysUser sysUser) {
        sysUserService.saveUser(sysUser);
        return Result.success(sysUser);
    }

    @Log(title = "用户管理", businessType = "修改状态")
    @PreAuthorize("hasAnyAuthority('system:sysUser:update')")
    @PostMapping("/updateStatus")
    public Result<SysUser> updateStatus(@RequestBody SysUser sysUser) {
        sysUserService.updateStatus(sysUser);
        return Result.success(sysUser);
    }

    @Log(title = "用户管理", businessType = "修改密码")
    @PostMapping("/updatePassword")
    public Result<SysUser> updatePassword(@RequestParam("oldPassword") String oldPassword,
                                          @RequestParam("newPassword") String newPassword) {
        return Result.success(sysUserService.updatePassword(oldPassword, newPassword));
    }

    @Log(title = "用户管理", businessType = "修改个人信息")
    @PostMapping("/updateUserProfile")
    public Result<SysUser> updateUserProfile(@RequestBody SysUser sysUser) {
        return Result.success(sysUserService.updateUserProfile(sysUser));
    }

    /**
     * 用户修改头像
     * @param file
     * @param id
     * @return
     */
    // todo 用消息队列异步处理删除用户原来的头像
    @Log(title = "用户管理", businessType = "修改头像")
    @PostMapping("/uploadAvatar")
    public Result<String> uploadAvatar(@RequestPart("file") MultipartFile file, @RequestParam("id") String id) {
        String url = sysUserService.uploadAvatar(file, id);
        return Result.success(url);
    }

    @Log(title = "用户管理", businessType = "删除")
    @PreAuthorize("hasAnyAuthority('system:sysUser:remove')")
    @DeleteMapping("/remove/{id}")
    public Result<SysUser> remove(@PathVariable String id) {
        sysUserService.removeUser(id);
        return Result.success();
    }

}

